package com.qch.edu.lagou.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qch
 * @Date 2021/7/22
 * 1.用邻接矩阵实现图
 */
public class Graph1 {
    //存储顶点的链表
    List vertextList;
    //二维数组 储存边 值是权值
    int[][] edges;
    //存储边数
    int numEdge;

    public Graph1(int n) {
        this.edges = new int[n][n];
        this.vertextList=new ArrayList();
        this.numEdge=0;
    }
    //获取节点个数
    public int getVertexNum()
    {
        return vertextList.size();
    }

    //得到边的数目
    public int getNumEdge() {
        return numEdge;
    }
    //返回节点i的数据
    public Object getVertex(int i)
    {
        return vertextList.get(i);
    }

    ///返回i,j的权值
    public int getWeight(int i , int j) {
        return edges[i][j];
    }

    //插入结点
    public void insertVertex(Object vertex) {
        vertextList.add(vertex);
    }

    /**
     * 插入边
     * @param i 顶点1
     * @param j 顶点二
     * @param weight 权值
     */
    public void insertEdge(int i, int j, int weight) {
        edges[i][j] = weight;
        numEdge++;
    }

    /**
     * (0)A      (3)D
     *
     * (1)B       (2)C
     * @param args
     */
    public static void main(String[] args) {
        Graph1 graph1 = new Graph1(4);
        graph1.insertVertex("A");//0
        graph1.insertVertex("B");//1
        graph1.insertVertex("C");//2
        graph1.insertVertex("D");//3
        //A-B 2
        graph1.insertEdge(0,1,2);
        //B-C 4
        graph1.insertEdge(1,2,4);
        //C-D 2
        graph1.insertEdge(2,3,2);
        //A-D 4
        graph1.insertEdge(0,3,4);
        System.out.println(graph1.getNumEdge());
        System.out.println(graph1.getVertexNum());
        System.out.println(graph1.getWeight(2,3));

    }

}
